
(function () {
    Codestrong.ui.createSettingMain= function () {
    
    var fontsizeDefault,fontsizeLarge,left
	
    	var aps='';
	if(Codestrong.isAndroid()){
		Titanium.include('../globals.js');
		 aps='../';
	} 
    else 	 Titanium.include('globals.js');
  
	if(Ti.Platform.osname == 'iphone' )
	{
	  	fontsizeDefault=Globals.fontsizeDefault_iphone;
	  	fontsizeLarge=Globals.fontsizeLarge_iphone;
	  	left=57
	}
	else
	{
	  	fontsizeDefault=Globals.fontsizeDefault_noniphone;
	  	fontsizeLarge=Globals.fontsizeLarge_noniphone;
	  	left=57
	}
    var topfirst,leftfirst,leftsecond,heightlabel,right,heighttext,space,bgcolor='#020202'	
        var btwidth=150;
		var btheight=45;
    if(Codestrong.isAndroid()) 
	{
		  topfirst=30,leftfirst=40,leftsecond=200,heightlabel=21,right=40,heighttext=50,space=10
	}
	else
	{
		  topfirst=30,leftfirst=40,leftsecond=200,heightlabel=21,right=40,heighttext=31,space=10
	}
  	var firstRun = true;
    var win = Titanium.UI.createWindow({
        id: 'SettingMain',
        title: 'CẤU HÌNH',
        backgroundColor: '#FFF',
        barColor: '#414444',
        fullscreen: false,
       // orientationModes:[Ti.UI.PORTRAIT]
    });
   
   
    
       
    var egc = new globals.EventGarbageCollection(win);
	
	 egc.addEventListener('back_SettingMain', function () {
    	Codestrong.navGroup.close(win, {
                        animated: true
        });
	});
  
	////////////main_header///////////////////////////
	 var databutton_right=[
    	//{Id:1,Image:aps+'images/list_bullets.png',Image_:aps+'images/list_bullets_.png',tag:'Category'},
	   // {Id:2,Image:aps+'images/account.png',Image_:aps+'images/account_.png',tag:'Login'},
	    //{Id:3,Image:aps+'images/search.png',Image_:aps+'images/search_.png',tag:'Search'},
	    
	]
	var main_header=require('controls/main_header').main_header({
	 		backgroundImage: aps+'images/panel3.png',
	 		headertext:'Thầy trò Mobile: Cấu hình',
		    left: 0,
		    top: 0,
		    height:65,
		    width: Globals.platformWidth,
		    touchEnabled: true		,
		   databutton_right:databutton_right,
		    imback:   aps+'images/back.png',
		    imback_:   aps+'images/back_.png',
		  	idWinParent:'SettingMain',
		    
    })

	win.add(main_header);
   
   
   
   
   
	////////////HEADER LectureDetailHeaderView////////////////////
	viewinfolecture=require('windows/SettingWindow_HeaderView').CreateSettingWindow_HeaderView({
		  			login_usernameinfo:Globals.login_usernameinfo,
		  			login_card:Globals.login_card,
			        top:main_header.top+main_header.height,
			        left:0,
			        height:110,
			    })
	win.add(viewinfolecture)
	//////////////////////////////////////////////////////////////	



    /////////////////////// ROW 2///////////////////
    var btRegister = Titanium.UI.createButton({
    	
		title:Globals.login_register,
		font:{
				fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
				//fontWeight:'bold'
			},
		backgroundImage:aps+Globals.backgroundImage_button,
		backgroundSelectedImage:aps+Globals.backgroundSelectedImage,
		backgroundDisabledImage: aps+Globals.backgroundDisabledImage,
		width:btwidth	,
		height:btheight	,
		left: leftfirst,
		top:viewinfolecture.top+viewinfolecture.height,
		color:Globals.foreColor
	});
	
	var createAlertConnectError = function()
	{
		var confirm = Titanium.UI.createAlertDialog({ 
						            title: Globals.title1, 
						            message:Globals.connecterror, 
						            buttonNames: [Globals.btOk]
						    });
		confirm.show();   
	}
	btRegister.addEventListener('click',function()
	{
		
		if (Ti.Network.online) {
			/////////Register Account////////
		 	if(btRegister.title==Globals.login_register)
			{
				//login
				var table = Ti.UI.createTableView();
			
				var RegisterPop = require('windows/RegisterPop').CreateRegisterPop
				var pop = new RegisterPop({
			        title: Globals.login_register,
			        view: table,
			        isRegister:1,
			        top:0
			    });
			    pop.open();
			}
			//////////Show Info//////////////
			else
			{
				var table = Ti.UI.createTableView();
			
				var RegisterPop = require('windows/RegisterPop').CreateRegisterPop
				var pop = new RegisterPop({
			        title: Globals.register_showInfo,
			        view: table,
			        isRegister:0,
			        //btLoginDialog:btLoginDialog,
			        top:0
			    });
			    pop.open();
			}
		 }
		 else
		 {
		  	   require('settings').createAlertConnectError();
		 }
	})
    win.add(btRegister);
    
    var btLogin	 = Titanium.UI.createButton({
		title:Globals.login_login,
		font:{
								fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
								//fontWeight:'bold'
			},
		backgroundImage:aps+Globals.backgroundImage_button,
		backgroundSelectedImage:aps+Globals.backgroundSelectedImage,
		backgroundDisabledImage: aps+Globals.backgroundDisabledImage,
		width:btwidth	,
		height:btheight	,
		center: 0,
		top:viewinfolecture.top+viewinfolecture.height,
		color:Globals.foreColor
	});
	 
   Ti.App.addEventListener('app:updateInfoLogin', function() {
		
		CheckLogin_Logout();
	});
	
	Ti.App.addEventListener('SettingWindow:updateInfo', function() {
		
		CheckLogin_Logout();
	});
  
	btLogin.addEventListener('click',function()
	{
		 if (Ti.Network.online) {
		 	if(btLogin.title==Globals.login_login)
			{
				//login
				var table = Ti.UI.createTableView();
			
				var LoginPop = require('windows/LoginPop').CreateLoginPop
				var pop = new LoginPop({
			        title: Globals.login_login,
			        view: table,
			        top:0,
			        isCheck:0//Login Setting
			    });
			    pop.open();
			}
			//////////Add Card//////////////
			else
			{
				var table = Ti.UI.createTableView();
			
				var AddCardPop = require('windows/AddCardPop').CreateAddCardPop
				var pop = new AddCardPop({
			        title: Globals.register_addcard,
			        view: table,
			        isRegister:0,
			        //btLoginDialog:btLoginDialog,
			        top:0
			    });
			    pop.open();
			}
		 }
		 else
		 {
		  	   require('settings').createAlertConnectError();
		 }
	})
    win.add(btLogin);
    
    var btForgotPass	 = Titanium.UI.createButton({
		title:Globals.login_forgotpass,
		font:{
								fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
								//fontWeight:'bold'
			},
		backgroundImage:aps+Globals.backgroundImage_button,
		backgroundSelectedImage:aps+Globals.backgroundSelectedImage,
		backgroundDisabledImage: aps+Globals.backgroundDisabledImage,
		width:btwidth	,
		height:btheight	,
		right: right	,
		top:viewinfolecture.top+viewinfolecture.height,
		color:Globals.foreColor
	});
	btForgotPass.addEventListener('click',function()
	{
		if (Ti.Network.online) {
			if(btForgotPass.title==Globals.login_logout)
			{
				//Is Login----->Logout
				var confirm = Titanium.UI.createAlertDialog({ 
						            title: Globals.title, 
						            message:'Bạn muốn đăng xuất khỏi tài khoản ?', 
						            buttonNames: [Globals.btOk,Globals.btCancel]
						    });
				confirm.show();   
				confirm.addEventListener('click',function(e) {
					//alert(e.index)
				     switch (e.index) {
				          case 0: {
				          		
				          		Ti.App.Properties.setInt('isLogin', 0);
				          		Ti.App.fireEvent('SettingWindow_HeaderView:updateInfo');
								btRegister.title=Globals.login_register;
								btLogin.title=Globals.login_login;
								btForgotPass.title=Globals.login_forgotpass;
								
				          }
				          break;
				 
				          //This will never be reached, if you specified cancel for index 1
				          case 1: {
				          	
				          }
				          break;
				 
				          default:
				          break;
				 
				      }
				});
			
				
			}
		 }
		 else
		 {
		  	   require('settings').createAlertConnectError();
		 }
		
	})
	
	
	var CheckLogin_Logout = function()
	{
		if(Ti.App.Properties.getInt('isLogin')==1)
		{
			Ti.App.fireEvent('SettingWindow_HeaderView:updateInfo');
			btRegister.title=Globals.login_showInfo;
			btLogin.title=Globals.login_cardInsert;
			btForgotPass.title=Globals.login_logout;
		}
		else
		{
			lbUserNameInfo.text =  Globals.login_usernameinfo;
			btRegister.title=Globals.login_register;
			btLogin.title=Globals.login_login;
			btForgotPass.title=Globals.login_forgotpass;
		}
	}
	
	
    win.add(btForgotPass);
    
    
    
    win.addEventListener('open', function()
	{
		 if (Ti.Network.online) {
		 	if(Ti.App.Properties.getInt('isLogin')==1)
			{
				Ti.App.fireEvent('SettingWindow_HeaderView:updateInfo');
				btRegister.title=Globals.login_showInfo;
				btLogin.title=Globals.login_cardInsert;
				btForgotPass.title=Globals.login_logout;
			}
		 }
	});
	
	
	
 	return win;
    };
})();